package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.MenuTitle;
import com.itheima.resources.MessageConstant;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user",name = "用户模块")
public class UserController {
    @RequestMapping(value = "/findUsername.do",name = "查询用户名")
    public Result findUsername(Authentication authentication){
        try {
            //获取用户名
            String name = authentication.getName();
            //返回用户名
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }

    }




    @Reference
    UserService userService;

    /**
     * 新增
     * @author Qifei
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        if(userService.findByUsername(user.getUsername())!=null){
            return new Result(false, "该用户名已被占用");
        }
        try {
            userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    /**
     * 修改
     * @author Qifei
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user, Integer[] roleIds){
        try {
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    /**
     * 分页查询
     * @author Qifei
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.findPage(queryPageBean);
    }

    /**
     * 删除
     * @author Qifei
     * @param userId
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer userId){
        try {
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            userService.delete(userId, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

    /**
     * 查找用户拥有的角色
     * @author Qifei
     * @param userId
     * @return
     */
    @RequestMapping("/findUserInfoByUserId")
    public Map findUserInfoByUserId(Integer userId){
        com.itheima.pojo.User user = userService.findByUserId(userId);
        List<Integer> roleIds = userService.findRoleIdsByUserId(userId);
        Map map = new HashMap();
        map.put("user", user);
        map.put("roleIds", roleIds);
        return map;
    }

    @RequestMapping(value = "/findAllMenuByRole.do")
    public Result findAllMenuByRole(Authentication authentication){
        try {
            //获取用户名
            String name = authentication.getName();
            List<MenuTitle> listMenu = userService.findAllMenuByRole(name);
            //返回用户名
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,listMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MENU_FAIL);
        }

    }

}
